package com.aletto.general_store.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LifecycleOwner;

import com.aletto.general_store.R;
import com.aletto.general_store.ui.adapters.GoodsCoverBannerAdapter;
import com.aletto.general_store.utils.GoodsCoverIndicator;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class PreviewGoodsActivity extends AppCompatActivity {

    public static Intent newIntent(
            Context context,
            CharSequence title,
            CharSequence price,
            ArrayList<String> list1, ArrayList<String> list2) {

        Intent intent = new Intent(context, PreviewGoodsActivity.class);
        intent.putExtra("TITLE", title.toString());
        intent.putExtra("PRICE", price.toString());
        intent.putStringArrayListExtra("LIST_1", list1);
        intent.putStringArrayListExtra("LIST_2", list2);
        return intent;
    }

    private ViewGroup layoutGoodsIntroduce;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_goods);
        XHelper.initTitle(this, R.string.title_preview_goods);

        // 定义
        TextView tvPrice = findViewById(R.id.tv_goods_price);
        TextView tvTitle = findViewById(R.id.tv_goods_title);
        layoutGoodsIntroduce = findViewById(R.id.layout_goods_introduce);
        Banner<String, GoodsCoverBannerAdapter> goodsCoverBanner = findViewById(R.id.banner_goods_cover);

        // 内容
        Intent intent = getIntent();
        ArrayList<String> list1 = intent.getStringArrayListExtra("LIST_1");
        ArrayList<String> list2 = intent.getStringArrayListExtra("LIST_2");

        // 初始化
        String price = intent.getStringExtra("PRICE");
        tvTitle.setText(intent.getStringExtra("TITLE"));
        tvPrice.setText(XHelper.toPrice(price));
        goodsCoverBanner
                .setAdapter(new GoodsCoverBannerAdapter(list1))
                .addBannerLifecycleObserver(this)
                .setIndicator(new GoodsCoverIndicator(this))
                .start();

        renderGoodsIntroduce(list2);
    }

    private void renderGoodsIntroduce(List<String> goodsIntroduce) {
        layoutGoodsIntroduce.removeAllViews();
        DisplayMetrics dm = getResources().getDisplayMetrics();

        if (goodsIntroduce != null && !goodsIntroduce.isEmpty()) {
            goodsIntroduce.forEach(s -> {
                ImageView imageView = new ImageView(layoutGoodsIntroduce.getContext());
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                layoutGoodsIntroduce.addView(imageView);

                GlideApp.with(imageView).asBitmap().load(s)
                        .into(new CustomTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                float ratio = resource.getWidth() / (float)resource.getHeight();
                                int imageViewHeight = (int) (dm.widthPixels / ratio);

                                imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, imageViewHeight));
                                imageView.setImageBitmap(resource);
                            }

                            @Override
                            public void onLoadCleared(@Nullable Drawable placeholder) {

                            }

                        });
            });
        }
    }

}
